package com.dd.shop4j.website.goods.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dd.shop4j.order.mapper.OrderDetailsMapper;
import com.dd.shop4j.order.mapper.OrderInfoMapper;
import com.dd.shop4j.order.model.OrderDetails;
import com.dd.shop4j.order.model.OrderInfo;
import com.dd.shop4j.website.controller.forms.OrderDetail;
import com.dd.shop4j.website.controller.forms.ShopcarItem;

@Service
@Transactional(rollbackFor = Exception.class)
public class OrderService {

	@Resource OrderInfoMapper orderInfoMapper;
	@Resource OrderDetailsMapper orderDetailsMapper;
	
	/**
	 * 新增订单信息
	 * @param form 商品表单
	 */
	public void insertGood(OrderDetail form) {
			OrderInfo orderInfo=new OrderInfo();
			orderInfo.setAddress(form.getDetailAddress());
			orderInfo.setAddTime(new Date());
			orderInfo.setContactsName(form.getName());
			orderInfo.setContactsTelphone(form.getTel());
			orderInfoMapper.insertSelective(orderInfo);
			ShopcarItem[] array=form.getShopcarItem();
			if(array!=null){
				List<OrderDetails> list=new ArrayList<OrderDetails>();
				for(ShopcarItem tmp:array){
					OrderDetails orderDetails=new OrderDetails();
					orderDetails.setGoodId(tmp.getGoodId());
					orderDetails.setOrderId(orderInfo.getId());
					orderDetailsMapper.insertSelective(orderDetails);
				}
			}
	}
	
}
